package com.goods.user.service;

import com.goods.entity.User;
import com.goods.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public Boolean getUserByCase(User user) {
        User u = userMapper.selectUserByCase(user);
        if (u != null) {
            return false;
        }
        return true;
    }

    @Transactional//写操作添加事务
    public boolean registUser(User user) {
        return userMapper.insert(user);
    }

    @Transactional//写操作添加事务
    public Map<String,Object> userActivate(User user) {
        Map<String, Object> map = new HashMap<>();
        String msg="激活码失败";
        Boolean status=false;

        if (user.getActivationcode()==null||user.getActivationcode()==""){
            msg="激活码为空";
            status=false;
        }else {
            User user1 = userMapper.selectUserByCase(user);
            if(user1==null){
                msg="激活码不正确";
                status=false;
            }else {
                if(user1.getStatus()){
                    msg= "激活重复";
                    status=false;
                }
                if (!user1.getStatus()){
//            激活
                    user1.setStatus(true);
                    int res= userMapper.updateByPrimaryKey(user1);
                    if (res==1){
                        msg="激活成功";
                        status=true;
                    }
                }
            }
        }

        map.put("msg",msg);
        map.put("status",status);
        return map;
    }

    public User userLogin(User user) {
        return userMapper.selectUserByCase(user);
    }

    @Transactional
    public void changePwd(User user) {
        userMapper.updateByPrimaryKey(user);
    }
}
